package com.csizg.facemodel.util;

import android.content.Context;
import android.support.annotation.RawRes;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * 获取raw工具类
 *
 * @author haohao(ronghao3508@gmail.com) on 2018/5/12 21:22
 * @version v1.0
 */
public class RawUtil {

    public static File getRawFile(Context context, @RawRes int rawName) {
        try {
            // load cascade file from application resources
            InputStream is = context.getResources().openRawResource(rawName);
            File cascadeDir = context.getDir("cascade", Context.MODE_PRIVATE);
            File rawFile = new File(cascadeDir, "raw_data");
            FileOutputStream os = new FileOutputStream(rawFile);

            byte[] buffer = new byte[4096];
            int bytesRead;
            while ((bytesRead = is.read(buffer)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            is.close();
            os.close();

            return rawFile;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
